import Vue from "vue";
import Vuex from "vuex";
import { userInfo } from "../api/user.js";
import bleManage from "./BluetoothManager.js";
import ringDataManage from "./RingDataManager.js";
import LOG4J from "../common/utils/Log4j";
import log4j from "./Log4j.js";

Vue.use(Vuex);

const store = new Vuex.Store({
	modules: {
		bleManage,
		ringDataManage,
		log4j,
	},
	state: {
		userInfo: uni.getStorageSync("userInfo") || {}, //用户信息
		userToken: uni.getStorageSync("userToken") || "", //用户token
		// Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjgyMzI1NCIsImlzcyI6IjgyMzI1NCJ9.a10OjmYZhiIktY83qez6K3zMoZ4lYdGypQLBHd7blBk
		// userToken: "",
		collectionToken: uni.getStorageSync("collectionToken") || "", // 藏品需要的token
		basePassword: "qsxz_", //用户基础安全密码
		shieldList: uni.getStorageSync("shieldList") || [], //屏蔽list
		giftType: uni.getStorageSync("giftType") || false, //判断是否赠送成功
		updateAnimation: uni.getStorageSync("updateAnimation") || false, //判断是否弹出动画
		searchHistory: uni.getStorageSync("searchHistory") || [], //用户好友搜索记录
		sleepData: uni.getStorageSync("sleep") || null,
		sleepConfig: {
			fall_asleep: 23,
			shortest: 7,
			shaking_num: 8,
			gravity_sensor: 5,
		},

		modules: ["bleManage", "ringDataManage", "log4j"],

		systemInfo: {
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
			platform: uni.getSystemInfoSync().platform,
			version: uni.getSystemInfoSync().version,
			language: uni.getSystemInfoSync().language,
			fontSizeSetting: uni.getSystemInfoSync().fontSizeSetting,
			screenWidth: uni.getSystemInfoSync().screenWidth,
			screenHeight: uni.getSystemInfoSync().screenHeight,
			pixelRatio: uni.getSystemInfoSync().pixelR,
		},

		bindMac: "",
		phoneOffsetMinutes: new Date().getTimezoneOffset(),
		ringInfo: {
			mac: "",
			name: "",
			customName: "",
		},

		ringUser: {
			userPoints: 0,
			lastUpDateTime: "",
		},

		hideRingPage: false
	},
	getters: {
		statusBarHeight: (state) => state.systemInfo.statusBarHeight,
		hasBindRing: (state) => !!state.bindMac,
		hideRing: (state) => state.hideRingPage,
	},
	mutations: {
		setHideRingPage(state, hide) {
			state.hideRingPage = hide;
		},
		//存储用户搜索记录
		setSearchHistory(state, search) {
			state.searchHistory = search;
			uni.setStorageSync("searchHistory", search);
		},
		//存储用户个人中心动画是否弹出
		setUpdateAnimation(state, updateType) {
			state.updateAnimation = updateType;
			uni.setStorageSync("updateAnimation", updateType);
		},
		//存储用户转增是否成功
		setGiftType(state, giftType) {
			state.giftType = giftType;
			uni.setStorageSync("giftType", giftType);
		},
		//存储用户信息
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			uni.setStorageSync("userInfo", userInfo);
		},

		//存储用户token
		setUserToken(state, token) {
			state.userToken = token;
			state.collectionToken = token.split(" ")[1];
			uni.setStorageSync("collectionToken", state.collectionToken);
			uni.setStorageSync("userToken", token);
		},

		//删除用户信息
		deleteUserInfo(state) {
			console.log("进入删除token");
			state.userToken = "";
			state.userInfo = {};
			state.collectionToken = "";
			state.shieldList = [];
			state.sleepData = null;
			state.sleepConfig = {
				fall_asleep: 23,
				shortest: 7,
				shaking_num: 8,
			};
			// uni.clearStorageSync();
			uni.removeStorageSync("userInfo");
			uni.removeStorageSync("userToken");
			uni.removeStorageSync("collectionToken");
			uni.removeStorageSync("sleep");
			uni.removeStorageSync("sleepId");
			uni.removeStorageSync("aheadFinish");
		},

		//保存屏蔽数据
		setShieldList(state, data) {
			state.shieldList.push(data);
			uni.setStorageSync("shieldList", state.shieldList);
		},

		SET_USER_INFO(state, info) {
			state.userInfo = info;
		},

		// 保存睡眠公用数据
		setSleepConfig(state, data) {
			state.sleepConfig = data;
		},

		// 保存睡眠目标
		setSleepTartget(state, data) {
			state.sleepData = data;
			uni.setStorageSync("sleep", data);
		},

		SET_RING_USER(state, user) {
			state.ringUser = Object.assign(state.ringUser, user);
		},
	},
	actions: {
		async getUserInfo({ commit }) {
			try {
				const res = await userInfo();
				commit("setUserInfo", res.data);
			} catch (e) {
				console.log(e);
				uni.showToast({
					title: e.data || e.message,
					icon: "none",
				});
			}
		},

		setRingUser({ commit }, user) {
			commit("SET_RING_USER", user);
		},
	},
});
LOG4J.setLogVx(store);

export default store;
